//package com.example.config;
//
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.context.annotation.Configuration;
//
//import javax.servlet.*;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
//@Configuration
//public class CorsFilter implements Filter {
//
//    @Override
//    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//        System.out.println("到这了");
//        HttpServletResponse rep = (HttpServletResponse) response;
//        HttpServletRequest req = (HttpServletRequest) request;
//
//        // 不使用*，自动适配跨域域名，避免携带Cookie时失效
//        String origin = req.getHeader("Origin");
////        if(StringUtils.isNotBlank(origin)) {
////            rep.setHeader("Access-Control-Allow-Origin", "http://localhost:9527");
////        }
//
//设置多域方法
//        String[] allowDomain= {"http://132.12.11.11:8888","http://123.112.112.12:80","http://123.16.12.23","http://121.12.18.13:10195"};
//        Set allowedOrigins= new HashSet(Arrays.asList(allowDomain));
//        String originHeader=((HttpServletRequest) req).getHeader("Origin");
//        if (allowedOrigins.contains(originHeader)){
//            ((HttpServletResponse) res).setHeader("Access-Control-Allow-Origin", originHeader);
//            ((HttpServletResponse) res).setContentType("application/json;charset=UTF-8");
//            ((HttpServletResponse) res).setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
//            ((HttpServletResponse) res).setHeader("Access-Control-Max-Age","3600");
//            ((HttpServletResponse) res).setHeader("Access-Control-Allow-Headers","Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With,userId,token");//表明服务器支持的所有头信息字段
//            ((HttpServletResponse) res).setHeader("Access-Control-Allow-Credentials", "true"); //如果要把Cookie发到服务器，需要指定Access-Control-Allow-Credentials字段为true;
//            ((HttpServletResponse) res).setHeader("XDomainRequestAllowed","1");
//        }
//

//
//        // 自适应所有自定义头
//        String headers = req.getHeader("Access-Control-Request-Headers");
//        if(StringUtils.isNotBlank(headers)) {
//            rep.setHeader("Access-Control-Allow-Headers", headers);
//            rep.setHeader("Access-Control-Expose-Headers", headers);
//        }
//
//        // 允许跨域的请求方法类型
//        rep.setHeader("Access-Control-Allow-Methods", "*");
//        // 预检命令（OPTIONS）缓存时间，单位：秒
//        rep.setHeader("Access-Control-Max-Age", "3600");
//        // 明确许可客户端发送Cookie，不允许删除字段即可
//        rep.setHeader("Access-Control-Allow-Credentials", "true");
//
//        chain.doFilter(req, rep);
//    }
//
//    @Override
//    public void init(FilterConfig filterConfig) {
//
//    }
//
//    @Override
//    public void destroy() {
//    }
//
//    /*
//    注册过滤器：
//    @Bean
//    public FilterRegistrationBean registerFilter() {
//        FilterRegistrationBean<CorsFilter> bean = new FilterRegistrationBean<>();
//        bean.addUrlPatterns("/*");
//        bean.setFilter(new CorsFilter());
//        // 过滤顺序，从小到大依次过滤
//        bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
//
//        return bean;
//    }
//     */
//}
